#include "stdafx.h"
#include "string_util.h"


#ifdef _UNICODE

/*
    //////////////////////////////////////////////////////////////////////////
    inline std::wstring toWideString(const std::string& str)
    {
        return toWideString(str.c_str(),(int)str.length()) ;
    }


    std::wstring toWideString(const char* pStr , int len)
    {
        ASSERT(pStr) ;
        ASSERT(len >= 0 || len == -1);

        // figure out how many wide characters we are going to get
        int nChars = MultiByteToWideChar(CP_ACP , 0 , pStr , len , NULL , 0) ;
        if (len == -1)
            -- nChars ;
        if (nChars == 0)
            return L"" ;

        // convert the narrow string to a wide string
        // nb: slightly naughty to write directly into the string like this
        wchar_t *wcstrTemp = new wchar_t[ nChars + 1 ];
        MultiByteToWideChar(CP_ACP , 0 , pStr , len , wcstrTemp , nChars) ;
        wcstrTemp [ nChars ] = 0;
        std::wstring buf(wcstrTemp);
        delete [] wcstrTemp;
        return buf;
    }


    inline std::string toNarrowString(const std::wstring& str)
    {
        return toNarrowString(str.c_str(),(int)str.length()) ;
    }

    std::string toNarrowString(const wchar_t* pStr , int len)
    {
        ASSERT(pStr) ;
        ASSERT(len >= 0 || len == -1);

        // figure out how many narrow characters we are going to get
        int nChars = WideCharToMultiByte(CP_ACP , 0 ,  pStr , len , NULL , 0 , NULL , NULL) ;
        if (len == -1) -- nChars ;
        if (nChars == 0) return "" ;

        // convert the wide string to a narrow string
        // nb: slightly naughty to write directly into the string like this
        char *wcstrTemp = new char[ nChars + 1 ];
        WideCharToMultiByte(CP_ACP , 0 , pStr , len , wcstrTemp , nChars, NULL , NULL) ;
        wcstrTemp [ nChars ] = 0;

        std::string buf(wcstrTemp);
        buf.resize(nChars) ;
        delete [] wcstrTemp;
        return buf;
    }
*/

/*
    mstring UtfToStr(const unsigned char* str)
    {
       
    }
*/

    std::wstring LoadResourceString(UINT uID)
    {
        CString temp;
        temp.LoadString(uID);
        return std::wstring(temp);

        //    LPCTSTR str = TEXT("");
        //    LoadString(NULL, uID, (LPTSTR)&str, 0);
        //    return std::wstring(str);
    }

#else

    mstring UtfToStr(const unsigned char* str)
    {
        return (const char*)str; 
    }

    std::string LoadResourceString(UINT uID)
    {
        CString temp;
        temp.LoadString(uID);
        return std::string(temp);

        //    LPCTSTR str = _T("");
        //    LoadString(NULL, uID, (LPTSTR)&str, 0);
        //    return std::string(str);
    }


#endif //unicode




DWORD Hex2DWORD(const char* pcstr)
{
    DWORD res = 0;

    while (*pcstr)
    {
        res <<=8;
        if (*pcstr>=_T('0') && *pcstr<=_T('9')) res |= (*pcstr-_T('0'));
        else if (*pcstr>=_T('a') && *pcstr<=_T('f')) res |= 10+(*pcstr-_T('a'));
        pcstr++;
    }
    return res;
}



